import java.util.Scanner;

public class BookManager {
    public static void main(String[] args) {
        sc = new Scanner(System.in);
        users = new String[20][3];
        books = new String[20][5];
        pubs = new String[20][3];
        loginUser = "";
        users[0][0] = "行政部";
        users[0][1] = "1";
        users[0][2] = "1";
        welcome();
    }

    static Scanner sc = new Scanner(System.in);
    static String[][] users;
    static String[][] books;
    static String[][] pubs;
    static String loginUser;

    private static void welcome() {
        while (true) {
            System.out.println("欢迎来到龙院图书管理系统");
            System.out.println("1.登录 2.注册 3.退出");
            int LogorReg = sc.nextInt();
            if (LogorReg == 1) {
                Log();
            } else if (LogorReg == 2) {
                Reg();
            } else {
                System.out.println("系统退出");
                System.exit(0);
            }
        }
    }


    private static void Log() {
        int Flag = 0;
        while (true) {
            System.out.println("请输入用户名");
            String userName = sc.next();
            System.out.println("请输入密码");
            String passWord = sc.next();
            for (int i = 0; i < users.length; ++i) {
                if (userName.equals(users[i][1]) && passWord.equals(users[i][2])) {
                    Flag = 1;
                }
            }
            if (Flag == 1) {
                System.out.println("登录成功！");
                loginUser = userName;
                homePage();
                break;
            } else {
                System.out.println("输入错误，请重新输入！");
            }
        }
    }


    private static void Reg() {
        System.out.println("请输入所属部门：");
        String deptName = sc.next();
        System.out.println("请输入用户名：");
        String userName = sc.next();
        System.out.println("请输入密码：");
        String passWord = sc.next();
        int i = getNullIndex(users);
        if (i == -1) {
            System.out.println("注册失败");
        } else {
            users[i][0] = deptName;
            users[i][1] = userName;
            users[i][2] = passWord;
            System.out.println("注册成功！");
        }
    }





//数组找空位子
     static int getNullIndex(String[][] arr) {
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i][0] == null) {
                index = i;
                break;
            }
        }
        return index;
    }


    private static void homePage() {
        System.out.println(loginUser + "，登入成功！欢迎您使用龙院书籍管理系统！！！");
        while(true) {
            System.out.println("请输入数字进行选择：1 图书管理  2 出版社管理  3 退出登录  4 退出系统");
            int num = sc.nextInt();
            if (num == 1) {
                bookManage();
            } else if (num == 2) {
            pubManage();
            } else if (num == 3) {
                loginUser = "";
                System.out.println("已退出登录！");
                welcome();
            } else {
                System.out.println("系统退出成功！");
                System.exit(0);
            }
        }

    }

    private static void bookManage() {
        while(true) {
            System.out.println("请输入：1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
            int num = sc.nextInt();
            if (num == 1) {
                addBook();
            } else if (num == 2) {
                delBook();
            } else if (num == 3) {
                updateBook();
            } else if (num == 4) {
                searchBook();
            } else {
                homePage();
            }
        }
    }
///看isbn是否有相同 没有的话index还是-1
    static int getIndexISBN(String isbn) {
        int index = -1;
        for(int i = 0; i < books.length; ++i) {
            if (isbn.equals(books[i][0])) {
                index = i;
                break;
            }
        }
        return index;
    }

    //看书名是否有相同 没有的话index还是-1
    private static int getIndexBookname(String bookname) {
        int index = -1;

        for(int i = 0; i < books.length; ++i) {
            if (bookname.equals(books[i][1])) {
                index = i;
                break;
            }
        }
        return index;
    }

    private static void addBook() {
        System.out.println("请输入图书ISBN:");
        String isbn = sc.next();
        System.out.println("请输入书名:");
        String bookName = sc.next();
        System.out.println("请输入价格:");
        String bookPrice = sc.next();
        System.out.println("请输入出版社:");
        String pub = sc.next();
        System.out.println("请输入作者:");
        String author = sc.next();
        boolean b = false;
        int i = getIndexISBN(isbn);
        if (i == -1) {
            int index = getNullIndex(books);
            books[index][0] = isbn;
            books[index][1] = bookName;
            books[index][2] = bookPrice;
            books[index][3] = pub;
            books[index][4] = author;
            b = true;
        }
        if (b) {
            System.out.println("添加成功！！！");
        } else {
            System.out.println("添加失败！！！该书已经存在！！！");
        }
    }

    private static void delBook() { System.out.println("请输入要删除的书名：");
        String BookName = sc.next();
        int i = getIndexBookname(BookName);
        if (i==-1) {
            System.out.println("删除失败,此书未入库");
        } else {
            books[i][0] = "";
            books[i][1] = "";
            books[i][2] = "";
            books[i][3] = "";
            books[i][4] = "";
            System.out.println("删除成功！");
        }
        }

    private static void updateBook() {
        System.out.println("请输入ISBN号：");
        String isbn = sc.next();
        int index = getIndexISBN(isbn);
        if (index != -1) {
            System.out.println("请输入要更新的书名：");
            String bookName = sc.next();
            System.out.println("请输入要更新的价格：");
            String bookPrice = sc.next();
            System.out.println("请输入要更新的出版社：");
            String pubCom = sc.next();
            System.out.println("请输入要更新的作者：");
            String author = sc.next();
            books[index][1] = bookName;
            books[index][2] = bookPrice;
            books[index][3] = pubCom;
            books[index][4] = author;
            System.out.println("更新成功！！！");
            }
         else {
            System.out.println("该ISBN号不存在！！！");
        }
    }

    private static void searchBook() {
        boolean a = true;

        while (true) {
            while (a) {
                System.out.println("请输入查询种类：1.ISBN 2.书名（模糊） 3.出版社 4. 作者 5. 价格范围 6.查询所有 7.返回上一级 ");
                int num = sc.nextInt();
                String w;
                if (num == 1) {
                    System.out.println("请输入ISBN号：");
                    w = sc.next();
                    searchBookByISBN(w);
                } else if (num == 2) {
                    System.out.println("请输入书名：");
                    w = sc.next();
                    searchByBookName(w);
                } else if (num == 3) {
                    System.out.print("请输入出版社：");
                    w = sc.next();
                    searchBookByPC(w);
                } else if (num == 4) {
                    System.out.println("请输入作者姓名：");
                    w = sc.next();
                    searchBookByAuthor(w);
                } else if (num == 5) {
                        System.out.println("请输入最低价格");
                        double min = sc.nextDouble();
                        System.out.println("请输入最高价格");
                        double max = sc.nextDouble();
                        searchBookByPrice(min, max);
                } else if (num == 6) {
                        searchAllBook();
                    }
                 else if(num == 7) {
                    a = false;
                }
                 else {
                    System.out.println("请输入正确的数字选项！！！");
                }
            }
            return;
        }
    }

        private static void searchBookByISBN(String isbn) {
            System.out.println("ISBN\t书名                       \t价格\t出版社           \t作者\t");

            for(int i = 0; i < books.length; ++i) {
                if (isbn.equals(books[i][0])) {
                    System.out.println(books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
                    break;
                }
            }

        }

        private static void searchByBookName(String name) {
            System.out.println("\tISBN\t书名                       \t价格\t出版社           \t作者\t");

            for (int i = 0; i < books.length; ++i) {
                if (books[i][0] != null && books[i][1].indexOf(name) != -1) {
                    System.out.println("\t" + books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
                }
            }
        }

    static void searchBookByPC(String pcName) {
        System.out.println("\tISBN\t书名                       \t价格\t出版社           \t作者\t");

        for(int i = 0; i < books.length; ++i) {
            if (pcName.equals(books[i][3])) {
                System.out.println("\t" + books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
            }
        }
    }

    static void searchBookByAuthor(String author) {
        System.out.println("\tISBN\t书名                       \t价格\t出版社           \t作者\t");

        for(int i = 0; i < books.length; ++i) {
            if (author.equals(books[i][4])) {
                System.out.println("\t" + books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
            }
        }

    }

    private static void searchBookByPrice(double min, double max) {
        System.out.println("ISBN\t书名                       \t价格\t出版社           \t作者\t");

        for(int i = 0; i < books.length; ++i) {
            if (books[i][0] != null) {
                double price = Double.parseDouble(books[i][2]);
                if (price >= min && price <= max) {
                    System.out.println(books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
                }
            }
        }
    }
    private static void searchAllBook() {
        System.out.println("ISBN\t书名                       \t价格\t出版社           \t作者\t");

        for(int i = 0; i < books.length; ++i) {
            if (books[i][0] != null) {
                System.out.println(books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
            }
        }
    }
//-------------------------------------------------------------------------------------------------------------------------------------------

    private static void pubManage() {
        while (true) {
            System.out.println("请输入：1.增加 2.删除 3.更新 4.根据出版社名称查询 5.查询所有出版社 6.返回上一级菜单");
            int num = sc.nextInt();
            if (num == 1) {
                addPub();
            } else if (num == 2) {
                delPub();
            } else if (num == 3) {
                updatePub();
            } else if (num == 4) {
                searchPubByName();
            } else if (num == 5) {
                searchAllPubCom();
            } else {
                homePage();
            }
        }
    }

    ///看出版社名是否有相同 没有的话index还是-1
    private static int getIndexpcName(String name) {
        int index = -1;

        for(int i = 0; i < pubs.length; ++i) {
            if (name.equals(pubs[i][0])) {
                index = i;
                break;
            }
        }

        return index;
    }

    private static int getIndexPubname(String pubname) {
        int index = -1;

        for(int i = 0; i < pubs.length; ++i) {
            if (pubname.equals(pubs[i][1])) {
                index = i;
                break;
            }
        }
        return index;
    }

    private static void addPub() {
        System.out.println("请输入出版社名称：");
        String pcName = sc.next();
        System.out.println("请输入出版社地址：");
        String pcAddress = sc.next();
        System.out.println("请输入出版社联系人：");
        String pcPerson = sc.next();
        boolean b = false;
        int i = getIndexpcName(pcName);
        if (i == -1) {
            int index = getNullIndex(pubs);
            pubs[index][0] = pcName;
            pubs[index][1] = pcAddress;
            pubs[index][2] = pcPerson;
            b = true;
        }
        if (b) {
            System.out.println("出版社添加成功！！！");
        } else {
            System.out.println("出版社添加失败!!!该出版社已存在！");
        }
    }

    private static void delPub() {
        System.out.println("请输入要删除的出版社名：");
        String pubName = sc.next();
        int i = getIndexPubname(pubName);
        if (i==-1) {
            System.out.println("删除失败,此出版社不存在");
        } else {
                pubs[i][0] ="";
                pubs[i][1] ="";
                pubs[i][2] ="";
                pubs[i][3] ="";
                pubs[i][4] ="";
                System.out.println("删除成功！");
            }
        }


    private static void updatePub() {
        System.out.println("请输入要更新的出版社名称：");
        String pubName = sc.next();
        int index = getIndexPubname(pubName);
        if(index !=-1) {
            System.out.println("请输入要更新的地址：");
            String pcAddress = sc.next();
            System.out.println("请输入要更新的联系人姓名：");
            String pcPerson = sc.next();
            pubs[index][1] = pcAddress;
            pubs[index][2] = pcPerson;
            System.out.println("更新成功");
        }
        else {
            System.out.println("更新失败！！！");
        }
    }

    private static void searchPubByName() {
        System.out.println("请输入出版社名称：");
        String pcName = sc.next();
        int index = getIndexpcName(pcName);
        if (index != -1) {
            System.out.println("出版社名称：" + pubs[index][0]);
            System.out.println("出版社地址：" + pubs[index][1]);
            System.out.println("出版社联系人：" + pubs[index][2]);
        } else {
            System.out.println("该出版社不存在！！！");
        }
    }

    private static void searchAllPubCom() {System.out.println("出版社名称\t地址 \t联系人\t");

        for(int i = 0; i < pubs.length; ++i) {
            if (pubs[i][0] != null) {
                System.out.println(pubs[i][0] + "\t" + pubs[i][1] + "\t" + pubs[i][2] + "\t");
            }
        }
    }

}







